package com.ccn.ui;

import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.os.Build;
import android.os.Bundle;
import android.view.DisplayCutout;
import android.view.View;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.widget.Button;

import com.ccn.ui.a2screenadaptation.A2_1_3PixelDensity;

public class A2Activity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //可以写一个Activity基类，在onCreate中调用；或者，在application中，添加Activity生命周期回调添加
//        A2_1_3PixelDensity.setDensity(getApplication(), this);
        //自定义View适配
//        setContentView(R.layout.activity_a2);
        //1.谷歌百分比布局2.手写百分比布局3.像素适配
//        setContentView(R.layout.activity_a2_percent);

        /*//刘海屏适配
        //设置全屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //还需要将window设置为全屏模式
        Window window = getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //华为、小米、oppo
        //1.判断手机厂商  2判断手机是否为刘海 3.设置是否让内容区域延伸进刘海 4.设置控件是否避开刘海区域 5.获取刘海高度

        //判断手机是否是刘海屏
        boolean hasDisplayCutout = hasDisplayCutout(window);

        if (hasDisplayCutout) {
            //2.让内容区域延伸进刘海区
            WindowManager.LayoutParams attributes = window.getAttributes();
            *//*
             * LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT 全屏模式 内容下移 非全屏模式不受影响
             * LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES 允许内容区域延伸到刘海区（需搭配沉浸式，否则仅显示根布局背景）
             * LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER不允许内容延伸进刘海
             * *//*
            attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            window.setAttributes(attributes);

            //3.设置成沉浸式
            int flags = View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            int visibility = window.getDecorView().getSystemUiVisibility();
            visibility |= flags;//追加沉浸式
            window.getDecorView().setSystemUiVisibility(visibility);
        }

        setContentView(R.layout.activity_a2_liuhai);
        //设置按钮向下移动
//        Button button = findViewById(R.id.heiheihei);
//        ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) button.getLayoutParams();
//        params.topMargin = heightForDisplayCutout();
//        button.setLayoutParams(params);

        //根布局设置头部padding
        ConstraintLayout layout = findViewById(R.id.container);
        layout.setPadding(0, heightForDisplayCutout(), 0, 0);*/
    }

    @RequiresApi(api = Build.VERSION_CODES.P)
    private boolean hasDisplayCutout(Window window) {
        DisplayCutout displayCutout;
        View rootView = window.getDecorView();
        WindowInsets insets = rootView.getRootWindowInsets();
        //Android9.0及以上才有刘海屏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && insets != null) {
            displayCutout = insets.getDisplayCutout();
            if (displayCutout != null) {
                if (displayCutout.getBoundingRects() != null
                        && displayCutout.getBoundingRects().size() > 0
                        && displayCutout.getSafeInsetTop() > 0) {
                    return true;
                }
            }
        }
        return false;//因为模拟器原因，这里设置为true，表示为刘海屏
    }

    /**
     * 通常情况下，刘海屏的高就是状态栏的高
     *
     * @return
     */
    public int heightForDisplayCutout() {
        int resID = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resID > 0) {
            return getResources().getDimensionPixelSize(resID);
        }
        return 0;
    }
}